package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:检查组服务
 * @Version: V1.0
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    //新增检查组,同时让检查组关联检查项
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组,操作t_checkgrouop表
        checkGroupDao.add(checkGroup);
        //设置检查组和检查项的多对多关联关系,操作t_checkgroup_checkitem表
        Integer checkGroupId = checkGroup.getId();
        if(checkitemIds !=null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer>map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }

        }
    }
    //
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page <CheckGroup> page = checkGroupDao.selectByCondition(queryString);
        List result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,result);
    }

    //查新所有检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


    //根据id删除检查组
    @Override
    public void deleteById(Integer id) {
        //计算当前检查组和套餐中间表是否存在关联
        long count = checkGroupDao.findCountByCheckGroupId(id);
        //判断当前检查组是否关联到套餐
        if(count > 0){
            //当前检查组已经关联到套餐,不允许删除
            new RuntimeException();
        }else {
            try {
                //没有关联到套餐,删除的检查组和检查项的外键关联
                checkGroupDao.deleteByCheckGroupId(id);
            } catch (Exception e) {
                //删除关联表失败,打印异常
                e.printStackTrace();
            }
            //关联表删除成功,删除当前检查组
            checkGroupDao.deleteById(id);
        }
    }

    //根据id查询检查组
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据检查组id查询关联检查项
    @Override
    public List<Integer> findCheckitemIdsByCheckgroupId(Integer id) {
        return checkGroupDao.findCheckitemIdsByCheckgroupId(id);
    }

    //编辑检查组信息,同时需要关联检查项
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组基本信息,操作检查组t_checkgroup表
        checkGroupDao.edit(checkGroup);
        //清理当前检查组关联的检查项,造作中间关系表t_checkgroup_checkitem表
        checkGroupDao.deleteAssoication(checkGroup.getId());
        //重新建立当前检查组和检查项的关联关系
        Integer checkGroupId = checkGroup.getId();
        if(checkitemIds !=null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer>map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
